package com.cqc.bmobdemo01.activity;

import android.app.ListActivity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.cqc.bmobdemo01.R;
import com.cqc.bmobdemo01.bean.Person;
import com.cqc.bmobdemo01.bean.User;
import com.cqc.bmobdemo01.util.ToastUtil;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends ListActivity {

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        //初始化
        Bmob.initialize(this, "b6cd889ac7adc3303ca9db6228fa7ff7","Bmob");

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.types, android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        switch (position) {
            case 0:
                saveData1();
                break;
            case 1:
                saveData2();
                break;
            case 2:
                queryData1();
                break;
            case 3:
                updateData();
                break;
            case 4:
                deleteData();
                break;
            case 5:
                savaBmobUser();
                break;
            default:
                ToastUtil.showShortToast(context, "switch  default");
                break;
        }
    }

    private void savaBmobUser() {
        User user_a = new User("chad", "河南");
        user_a.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                showResult(objectId, e);
            }
        });
    }

    private void deleteData() {
        final Person person = new Person();
//        person.setObjectId("0a9f557a59");
        person.delete("0a9f557a59", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    ToastUtil.showShortToast(context, "删除数据成功," + person.getUpdatedAt());
                } else {
                    ToastUtil.showShortToast(context, "删除数据失败，e=" + e.getMessage());
                }
            }
        });
    }

    private void updateData() {
        final Person person = new Person("Bob", "江苏");
        person.update("29c3ea62ef", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    ToastUtil.showShortToast(context, "修改数据成功,objectId=" + person.getUpdatedAt());
                } else {
                    ToastUtil.showShortToast(context, "修改数据失败，e=" + e.getMessage());
                }
            }
        });
    }

    private void queryData1() {
        BmobQuery<Person> query = new BmobQuery<>();
        query.getObject("5985d31543", new QueryListener<Person>() {
            @Override
            public void done(Person person, BmobException e) {
                if (e == null) {
                    ToastUtil.showShortToast(context, "查询数据成功,objectId=" + person.getName());
                } else {
                    ToastUtil.showShortToast(context, "查询数据失败，e=" + e.getMessage());
                }
            }
        });
    }

    private void saveData1() {
        Person person_a = new Person("Mike", "山东");
        person_a.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                showResult(objectId, e);
            }
        });
    }

    private void saveData2() {
        Person person_b = new Person("Jean", "河南");
        person_b.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                showResult(objectId, e);
            }
        });
    }

    private void showResult(String objectId, BmobException e) {
        if (e == null) {
            ToastUtil.showShortToast(context, "添加数据成功,objectId=" + objectId);
        } else {
            ToastUtil.showShortToast(context, "添加数据失败，e=" + e.getMessage());
        }
    }
}
